﻿using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.ORG.DB
{
    public class DB_User : BaseDBML<Frame_User>
    {
        [AutoWired]
        private DB_OU @DB_OU;
        [AutoWired]
        private DB_UserExtend @DB_UserExtend;
        [AutoWired]
        private DB_Config @DB_Config;
        [AutoWired]
        private DB_Login @DB_Login;
        public override void Insert(Frame_User Entity)
        {
            Frame_OU OU = DB_OU.SelectOne(Entity.OUGuid);
            if (string.IsNullOrEmpty(Entity.UserGuid))
            {
                Entity.UserGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.Password = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + Entity.Password);
            Entity.AddDate = DateTime.Now;
            Entity.UpdateDate = DateTime.Now;
            base.Insert(Entity);
            //同步到Frame_Login
            Frame_Login Login = new Frame_Login();
            Login.LoginID = Entity.LoginID;
            Login.Password = Entity.Password;
            Login.UserGuid = Entity.UserGuid;
            Login.UserName = Entity.DisplayName;
            Login.Enable = Entity.Enable;
            Login.SyncFrom = (int)Base.Enum.ORG.SourceFrom.组织架构人员;
            @DB_Login.Insert(Login);
            if (OU != null && !string.IsNullOrEmpty(OU.OUGuid))
            {
                //计算部门下人员
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "OUGuid", ParamValue = OU.OUGuid } };
                OU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"), ParamList);
                @DB_OU.UpdateByID(OU);
            }

        }

        public override void UpdateByID(Frame_User Entity)
        {
            Frame_User DBUser = SelectOne(Entity.UserGuid);
            Entity.UpdateDate = DateTime.Now;
            base.UpdateByID(Entity);
            if (DBUser.OUGuid != Entity.OUGuid)
            {
                //计算部门下人员
                Frame_OU DBOU = DB_OU.SelectOne(DBUser.OUGuid);
                if (DBOU != null && !string.IsNullOrEmpty(DBOU.OUGuid))
                {
                    List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "OUGuid", ParamValue = DBOU.OUGuid } };
                    DBOU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"), ParamList);
                    DB_OU.UpdateByID(DBOU);

                }
                Frame_OU NowOU = DB_OU.SelectOne(Entity.OUGuid);
                if (NowOU != null && !string.IsNullOrEmpty(NowOU.OUGuid))
                {
                    List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "OUGuid", ParamValue = NowOU.OUGuid } };
                    NowOU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"), ParamList);
                    DB_OU.UpdateByID(NowOU);
                }


            }
            //同步更新Frame_Login
            Frame_Login Login = new Frame_Login();
            Login.UserGuid = Entity.UserGuid;
            Login.UserName = Entity.DisplayName;
            Login.Enable = Entity.Enable;
            @DB_Login.UpdateByID(Login);
        }

        public override void DeleteByID(string UnitGuid)
        {
            Frame_User DBUser = SelectOne(UnitGuid);
            Frame_OU OU = DB_OU.SelectOne(DBUser.OUGuid);
            base.DeleteByID(UnitGuid);
            DB_UserExtend.DeleteByID(UnitGuid);
            if (OU != null && !string.IsNullOrEmpty(OU.OUGuid))
            {
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "OUGuid", ParamValue = OU.OUGuid } };
                OU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"), ParamList); ;
                DB_OU.UpdateByID(OU);
            }
            //同步删除Frame_Login
            @DB_Login.DeleteByID(UnitGuid);
        }

        public void InitPWD(string UnitGuid)
        {
            Frame_User Entity = SelectOne(UnitGuid);
            Entity.Password = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + DB_Config.SelectConfigByName("DefaultPWD").ConfigValue);
            base.UpdateByID(Entity);
            Frame_Login LoginEntity = DB_Login.SelectOne(UnitGuid);
            LoginEntity.Password = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + DB_Config.SelectConfigByName("DefaultPWD").ConfigValue);
            DB_Login.UpdateByID(LoginEntity);
        }

    }
}
